﻿using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraCharts;

namespace TaxiDispatchClient
{
    public partial class TaxiRouteStatisticsReportForm : DevExpress.XtraEditors.XtraForm
    {
        public TaxiRouteStatisticsReportForm()
        {
            InitializeComponent();
           
            dateEdit1.DateTime = DateTime.Now;

            int yearNow = DateTime.Now.Year;             
            do
            {
                string item = yearNow + "年";
                comBox_startYear.Properties.Items.Add(item);
                comBox_endYear.Properties.Items.Add(item);
                yearNow--;
            } while (yearNow>=2012);
            
            int monthNow = DateTime.Now.Month;
            comBox_endMonth.Text = monthNow + "月";
            comBox_startYear.Text = DateTime.Now.AddMonths(-3).ToString("yyyy")+"年";
            comBox_startMonth.Text = DateTime.Now.AddMonths(-3).ToString("MM") + "月";
            comBox_endYear.Text = DateTime.Now.ToString("yyyy") + "年";
            comBox_endMonth.Text = DateTime.Now.ToString("MM") + "月";

            dateEdit1.DateTime = DateTime.Now.AddDays(-10);
            dateEdit2.DateTime = DateTime.Now;
            dateEdit1.Location = comBox_startYear.Location;
            dateEdit2.Location = comBox_endYear.Location;
            //chartControl1.Series[0].ArgumentScaleType = ScaleType.Qualitative;
            //chartControl1.Series[0].ValueScaleType = ScaleType.Numerical;
            //chartControl1.Series[0].PointOptions.PointView = PointView.ArgumentAndValues;
            //chartControl1.Series[0].PointOptions.ValueNumericOptions.Format = NumericFormat.Percent;

        }

        private string _title = "";
        private int _dtType = 0;
        private string _ds1Title = "";
        private Thread thread;
        private void btn_getResult_Click(object sender, EventArgs e)
        {
            if (CheckIsValid())
            {
                
                chartControl2.Titles[0].Text = "统计中...";
                thread = new Thread(GetResult);
                thread.IsBackground = true;
                thread.Start();
            }
        }

        private bool CheckIsValid()
        {
            if (combox_taxi.SelectedIndex==0)
            {
                if (textEdit_taxi.Text.Trim() == "")
                {
                    XtraMessageBox.Show("请选择想要查询的出租车！");
                    textEdit_taxi.Focus();
                    return false;
                }
            }
            switch (combox_dateType.SelectedIndex)
            {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
            }
            return true;
        }

        private void GetResult()
        {
            #region 检验查询条件 并得出结果

            try
            {
             //   DataSet ds = MainForm.PositionService.TaxiRouteGetStatisReportResult(
             //1, "", 1, "2013-05-04 00:00:00", "2013-05-08 00:00:00");
                int objValue=0;
                string strValue="";
                int dateType=0;
                string startTime="";
                string endTime="";
                switch (combox_taxi.SelectedIndex)
                {
                    case 0: //单量车
                        objValue = 0;
                        strValue = textEdit_taxi.Text;
                        dateType = combox_dateType.SelectedIndex;
                        startTime = dateEdit1.DateTime.Date + " 00:00:00";
                        endTime = dateEdit2.DateTime.Date + "23：59：59";
                        break;
                    case 1:
                        objValue = 1;
                        strValue = "";
                        dateType = combox_dateType.SelectedIndex;
                        
                        startTime = comBox_startYear.Text.Replace("年", "-") +
                                    comBox_startMonth.Text.Replace("月", "-") + "01"
                                    +" 00:00:00";
                        string endMonth = comBox_endYear.Text.Replace("年", "-") +
                                         comBox_endMonth.Text.Replace("月", "-") + "01";
                        DateTime endDate = DateTime.Parse(endMonth).AddMonths(1).AddDays(-1);
                        endTime = endDate.ToString("yyyy-MM-dd") + " 23:59:59";


                        if (MainForm.MyUserInfo.CompanyName!=Service.PowerConfig.MainCompanyName)
                        {
                            endTime += "' and CompanyName='" + MainForm.MyUserInfo.CompanyName;
                        }

                        break;
                }
                 
                DataSet ds1 = new DataSet();
                DataSet ds2 = new DataSet();
                
                
                Invoke(new MethodInvoker(() => { btn_getResult.Enabled = false; }));
                try
                {
                    //ds1 = MainForm.PositionService.TaxiRouteGetStatisReportResult(
                    //    objValue,strValue, dateType, startTime, endTime); 
                    ds2 = MainForm.PositionService.TaxiRouteGetStatisReportResult(
                        objValue, strValue, dateType, startTime, endTime);
                }
                catch (Exception ex)
                {
                    if (!ex.Message.Contains("正在中止线程"))
                    {
                        Common.CommonRoutine.HandlingExceptions(ex, true);
                        Invoke(new MethodInvoker(() => { btn_getResult.Enabled = true; }));
                    }
                    return;
                }
            #endregion

                Invoke(new MethodInvoker(() =>
                {
                   
                    chartControl2.DataSource = null;
                }));
                //chartControl1.Series.Clear();
                Series series = new Series("饼图统计", ViewType.Pie3D);
                //chartControl1.Series.Add(series);
                //chartControl1.Series[0].Points.Clear();

                //总结果报表chartControl1赋值
                double carrypert = 0;
                double nopert = 0;
                 
                try
                {

                    //if (ds1 != null && ds1.Tables.Count == 1 && ds1.Tables[0].Rows.Count > 0)
                    //{
                    //    carrypert = double.Parse(ds1.Tables[0].Rows[0]["KongZai"].ToString());
                    //    nopert = double.Parse(ds1.Tables[0].Rows[0]["ZaiKe"].ToString());                         
                        
                    //    //if (chartControl1.Series[0].Points.Count == 0)
                    //    //{
                    //    SeriesPoint seriesPoint = new SeriesPoint("载客率", carrypert);
                    //    series.Points.Add(seriesPoint);
                    //    seriesPoint = new SeriesPoint("空载率", nopert);
                    //    series.Points.Add(seriesPoint);
                         
                    //    //}
                    //    //else
                    //    //{
                    //    //    chartControl1.Series[0].Points[0].Values = new double[] { carrypert };
                    //    //    chartControl1.Series[0].Points[1].Values = new double[] { nopert };
                    //    //    chartControl1.Series[0].Points[2].Values = new double[] { otherpert };
                    //    //}
                    //    // BeginInvoke(new MethodInvoker(() => { chartControl1.Show(); }));
                    //}

                    
                    //详细报表chartControl2赋值
                    if (ds2 != null && ds2.Tables.Count == 1)
                    {
                        Invoke(new MethodInvoker(() =>
                        {
                            chartControl2.DataSource = ds2.Tables[0];
                            chartControl2.Titles[0].Text = _title;
                            chartControl2.Series[0].ArgumentDataMember = "DT";
                            chartControl2.Series[0].ValueDataMembers.AddRange(new string[] { "KongZai" });
                            chartControl2.Series[1].ArgumentDataMember = "DT";
                            chartControl2.Series[1].ValueDataMembers.AddRange(new string[] { "ZaiKe" });                            
                        }));
                    }
                }
                catch (Exception ex)
                {
                    Common.CommonRoutine.HandlingExceptions(ex);
                }

                BeginInvoke(new MethodInvoker(() => { btn_getResult.Enabled = true; }));
            }
            catch (Exception ex)
            {

                Common.CommonRoutine.HandlingExceptions(ex);
                Console.WriteLine(ex);
            }

        }
         
        //出租车对象选择
        private void combox_taxi_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (combox_taxi.SelectedIndex)
            {
                case 0:
                    textEdit_taxi.Visible = true;
                    btn_selectTaxi.Visible = true;
                    break;               
                case 1:
                  textEdit_taxi.Visible = false;
                    btn_selectTaxi.Visible = false;
                    break;
            }

        }

        private void combox_dateType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (combox_dateType.SelectedIndex)
            {
                case 0://天
                    comBox_startYear.Visible = false;
                    comBox_startMonth.Visible = false;
                    comBox_endYear.Visible = false;
                    comBox_endMonth.Visible = false;
                    dateEdit1.Visible = true;
                    dateEdit2.Visible = true;
                    dateEdit1.Location = comBox_startYear.Location;
                    dateEdit2.Location = comBox_endYear.Location;
                    break;
                case 1://月
                    comBox_startYear.Visible = true;
                    comBox_startMonth.Visible = true;
                    comBox_endYear.Visible = true;
                    comBox_endMonth.Visible = true;
                    dateEdit1.Visible = false;
                    dateEdit2.Visible = false;
                    break;                
            }

        }


        //选着单个出租车
        private void btn_selectTaxi_Click(object sender, EventArgs e)
        {
            DriverInfoForm driverInfoForm = new DriverInfoForm();
            driverInfoForm.ShowDialog();
            if (driverInfoForm.gridView1.FocusedRowHandle >= 0)
            {
                DataRow row = driverInfoForm.gridView1.GetDataRow(driverInfoForm.gridView1.FocusedRowHandle);
                textEdit_taxi.Text = row["phoneNumber"].ToString();
                //label_driver.Text = row["Name"].ToString();                               
            }
        }

        #region 窗体控件效果
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;

            Cursor = Cursors.Arrow;

        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void TaxiStateStatisticsReportForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }

        #endregion

        private void btn_seeAllTaxiStati_Click(object sender, EventArgs e)
        {
            TaxiStateStatisticsForm taxiStateStatistics = new TaxiStateStatisticsForm();
            taxiStateStatistics.ShowDialog();
        }

        private void TaxiStateStatisticsReportForm_Load(object sender, EventArgs e)
        {
            btn_getResult_Click(null, null);
        }

        private void TaxiStateStatisticsReportForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (thread != null)
            {
                thread.Abort();
            }
        }

   

    }
}